/*

  Simple cross-product, outputs in correct triangular form.

  - Coalescing memory access in all reads
  - No memory coalscing in writes
  - Shared memory reads of type float2 to reduce global memory traffic
  - Each thread works on a 2x1 square of data

 */

// Read in column in first warp as float2, row in second warp
#define LOAD_ONE_BY_TWO(s)					\
  {*(input##s##_p) = *array_p;					\
    CUBE_ADD_BYTES(4);}

// read in shared data as individual floats to avoid bank conflicts

#define ONE_BY_TWO_COMPUTE(s)						\
  {float col1Xreal = input[s][colIdx];					\
  float col1Ximag = input[s][colIdx + 1];				\
  float col1Yreal = input[s][colIdx + 2];				\
  float col1Yimag = input[s][colIdx + 3];				\
  float col2Xreal = input[s][colIdx + 4];				\
  float col2Ximag = input[s][colIdx + 5];				\
  float col2Yreal = input[s][colIdx + 6];				\
  float col2Yimag = input[s][colIdx + 7];				\
  float row1Xreal = input[s][rowIdx];					\
  float row1Ximag = input[s][rowIdx + 1];				\
  float row1Yreal = input[s][rowIdx + 2];				\
  float row1Yimag = input[s][rowIdx + 3];				\
  sum11XXreal += row1Xreal * col1Xreal;					\
  sum11XXreal += row1Ximag * col1Ximag;					\
  sum11XXimag += row1Ximag * col1Xreal;					\
  sum11XXimag -= row1Xreal * col1Ximag;					\
  sum11XYreal += row1Xreal * col1Yreal;					\
  sum11XYreal += row1Ximag * col1Yimag;					\
  sum11XYimag += row1Ximag * col1Yreal;					\
  sum11XYimag -= row1Xreal * col1Yimag;					\
  sum11YXreal += row1Yreal * col1Xreal;					\
  sum11YXreal += row1Yimag * col1Ximag;					\
  sum11YXimag += row1Yimag * col1Xreal;					\
  sum11YXimag -= row1Yreal * col1Ximag;					\
  sum11YYreal += row1Yreal * col1Yreal;					\
  sum11YYreal += row1Yimag * col1Yimag;					\
  sum11YYimag += row1Yimag * col1Yreal;					\
  sum11YYimag -= row1Yreal * col1Yimag;					\
  sum12XXreal += row1Xreal * col2Xreal;					\
  sum12XXreal += row1Ximag * col2Ximag;					\
  sum12XXimag += row1Ximag * col2Xreal;					\
  sum12XXimag -= row1Xreal * col2Ximag;					\
  sum12XYreal += row1Xreal * col2Yreal;					\
  sum12XYreal += row1Ximag * col2Yimag;					\
  sum12XYimag += row1Ximag * col2Yreal;					\
  sum12XYimag -= row1Xreal * col2Yimag;					\
  sum12YXreal += row1Yreal * col2Xreal;					\
  sum12YXreal += row1Yimag * col2Ximag;					\
  sum12YXimag += row1Yimag * col2Xreal;					\
  sum12YXimag -= row1Yreal * col2Ximag;					\
  sum12YYreal += row1Yreal * col2Yreal;					\
  sum12YYreal += row1Yimag * col2Yimag;					\
  sum12YYimag += row1Yimag * col2Yreal;					\
  sum12YYimag -= row1Yreal * col2Yimag;}


/*  sum11XXreal += row1Xreal * col1Xreal + row1Ximag * col1Ximag;	\
  sum11XXimag += row1Ximag * col1Xreal - row1Xreal * col1Ximag;		\
  sum11XYreal += row1Xreal * col1Yreal + row1Ximag * col1Yimag;		\
  sum11XYimag += row1Ximag * col1Yreal - row1Xreal * col1Yimag;		\
  sum11YXreal += row1Yreal * col1Xreal + row1Yimag * col1Ximag;		\
  sum11YXimag += row1Yimag * col1Xreal - row1Yreal * col1Ximag;		\
  sum11YYreal += row1Yreal * col1Yreal + row1Yimag * col1Yimag;		\
  sum11YYimag += row1Yimag * col1Yreal - row1Yreal * col1Yimag;		\
  sum12XXreal += row1Xreal * col2Xreal + row1Ximag * col2Ximag;		\
  sum12XXimag += row1Ximag * col2Xreal - row1Xreal * col2Ximag;		\
  sum12XYreal += row1Xreal * col2Yreal + row1Ximag * col2Yimag;		\
  sum12XYimag += row1Ximag * col2Yreal - row1Xreal * col2Yimag;		\
  sum12YXreal += row1Yreal * col2Xreal + row1Yimag * col2Ximag;		\
  sum12YXimag += row1Yimag * col2Xreal - row1Yreal * col2Ximag;		\
  sum12YYreal += row1Yreal * col2Yreal + row1Yimag * col2Yimag;		\
  sum12YYimag += row1Yimag * col2Yreal - row1Yreal * col2Yimag;}*/


CUBE_KERNEL(shared1x2float, cuFloatComplex *array_d, cuFloatComplex *product_d, 
	    const int write)
{
  CUBE_START;

  //get local thread ID
  int ty = threadIdx.y;
  int tx = threadIdx.x;
  int tid = ty*TILE_WIDTH + tx;

  //set frequency number from blockIdx.y
  int f = blockIdx.y;

  int Row, Col, blockX, blockY;
  CUBE_DEVICE_CALL(findPosition, Col, Row, blockX, blockY);
  Col*=2;

  //declare shared memory for input coalescing
  #define PAD 1
  __shared__ float input[2][4*(32+PAD)]; // 4* for float4, 2* for 2x2 tile size

  //instantiate sum variables
  float sum11XXreal = 0.0, sum11XXimag = 0.0;
  float sum11XYreal = 0.0, sum11XYimag = 0.0;
  float sum11YXreal = 0.0, sum11YXimag = 0.0;
  float sum11YYreal = 0.0, sum11YYimag = 0.0;
  float sum12XXreal = 0.0, sum12XXimag = 0.0;
  float sum12XYreal = 0.0, sum12XYimag = 0.0;
  float sum12YXreal = 0.0, sum12YXimag = 0.0;
  float sum12YYreal = 0.0, sum12YYimag = 0.0;

  float *array_p = (float*)array_d + tid;
  if (tid < 8*TILE_WIDTH) {
    array_p += (f*NSTATION*NPOL + 2*blockX*TILE_WIDTH*NPOL)*2; //we're dealing with floats instead of float2s
  } else {
    array_p += (f*NSTATION*NPOL + 2*blockY*TILE_WIDTH*NPOL)*2 - 8*TILE_WIDTH;    
  }

  // introduce colIdx and rowIdx variables for conflict-free reading
  int colIdx = (tx/4)*(32+PAD) + 8*(tx%4);
  int rowIdx = (ty/8)*(32+PAD) + 4*(ty%8) + 2*(32 + PAD);

  // introduce new tid value for conflict-free writing
  tid = (32 + PAD)*(ty/4) + 8*(ty%4) + tx;

  float *input0_p = input[0] + tid;
  float *input1_p = input[1] + tid;

  LOAD_ONE_BY_TWO(0);

  //#pragma unroll 4
  for (int t=0; t<NTIME-2; t+=2){
    __syncthreads();
    ONE_BY_TWO_COMPUTE(0);
    array_p += NFREQUENCY*NSTATION*NPOL*2;

    LOAD_ONE_BY_TWO(1);
    __syncthreads();
    ONE_BY_TWO_COMPUTE(1);
    array_p += NFREQUENCY*NSTATION*NPOL*2;

    LOAD_ONE_BY_TWO(0);
  }

  __syncthreads();
  ONE_BY_TWO_COMPUTE(0);
  array_p += NFREQUENCY*NSTATION*NPOL*2;

  LOAD_ONE_BY_TWO(1);
  __syncthreads();
  ONE_BY_TWO_COMPUTE(1);

  if (write)
    CUBE_DEVICE_CALL(write1x2, Col, Row, product_d, sum11XXreal, sum11XXimag, sum11XYreal, 
		     sum11XYimag, sum11YXreal, sum11YXimag, sum11YYreal, sum11YYimag, 
		     sum12XXreal, sum12XXimag, sum12XYreal, sum12XYimag, sum12YXreal, 
		     sum12YXimag, sum12YYreal, sum12YYimag);

  if (Col < Row) {
    CUBE_ADD_BYTES(64);
    CUBE_ADD_FLOPS(NTIME*64);
  } else {
    CUBE_ADD_BYTES(32);
    CUBE_ADD_FLOPS(NTIME*32);
  }

  CUBE_END;
}

